import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()
root.title("网络调试工具 v0.0.1")
# root.geometry("800x600")  # 添加合适的初始窗口大小

"""左边布局"""
left_frame = tk.Frame(root, width=250)
left_frame.pack(side="left", anchor="n", padx=5, fill="y")
left_frame.pack_propagate(False)

"""左边布局 网络设置"""
net_frame = tk.LabelFrame(left_frame, text="网络设置", padx=5, pady=5)
net_frame.pack(side="top", anchor="n", padx=5, pady=5, fill="x")

tk.Label(net_frame, text='(1)网络协议', padx=5).pack(side="top", anchor="w")
scoket_type = ttk.Combobox(net_frame)
scoket_type['values'] = ['TCP 服务器', 'TCP 客户端', 'UDP 服务器', 'UDP 客户端']
scoket_type.current(0)
scoket_type.pack(side="top", anchor="w", padx=5, fill="x")

tk.Label(net_frame, text='(2)本地主机地址', padx=5).pack(side="top", anchor="w")
ip_combo = ttk.Combobox(net_frame)
ip_combo['values'] = ['127.0.0.1', '192.168.1.100', '192.168.1.101', '192.168.0.1']
ip_combo.current(0)
ip_combo.pack(side="top", anchor="w", padx=5, fill="x")

tk.Label(net_frame, text='(3)本地端口', padx=5).pack(side="top", anchor="w")
enter_port = tk.Entry(net_frame)
enter_port.insert(0, "8080")  # 添加默认端口示例
enter_port.pack(side="top", padx=5, anchor="w", fill="x")

tk_btn = tk.Frame(net_frame)
tk_btn.pack(side="top")

open_btn = tk.Button(tk_btn, text='打开').pack(side="left", padx=5, anchor="w")
cancel_btn = tk.Button(tk_btn, text='关闭').pack(side="right", padx=5, anchor="e")

"""接收设置"""
rev_frame = tk.LabelFrame(left_frame, text='接收设置', padx=5, pady=5)
rev_frame.pack(fill="x", padx=5, pady=5)

encoding_var = tk.StringVar(value="UTF-8")
tk.Radiobutton(rev_frame, text="UTF-8", variable=encoding_var, value="UTF-8").pack(anchor="w")
tk.Radiobutton(rev_frame, text="GBK", variable=encoding_var, value="GBK").pack(anchor="w")
tk.Checkbutton(rev_frame, text='自动解析JSON').pack(anchor="w")
tk.Checkbutton(rev_frame, text='接收时自动换行').pack(anchor="w")
tk.Checkbutton(rev_frame, text='显示时间戳').pack(anchor="w")

"""发送设置"""
send_set_frame = tk.LabelFrame(left_frame, text='发送设置', padx=5, pady=5)
send_set_frame.pack(fill="x", padx=5, pady=5)

send_encoding_var = tk.StringVar(value="UTF-8")
tk.Radiobutton(send_set_frame, text="UTF-8", variable=send_encoding_var, value="UTF-8").pack(anchor="w")
tk.Radiobutton(send_set_frame, text="GBK", variable=send_encoding_var, value="GBK").pack(anchor="w")
tk.Checkbutton(send_set_frame, text='发送前加密').pack(anchor="w")
tk.Checkbutton(send_set_frame, text='自动添加换行').pack(anchor="w")
tk.Checkbutton(send_set_frame, text='发送后清空').pack(anchor="w")

"""右边布局"""
right_frame = tk.Frame(root)
right_frame.pack(side="right", expand=True, padx=5, pady=5, fill="both")

"""右边布局 数据日志"""
info_frame = tk.Frame(right_frame)
info_frame.pack(anchor="nw", side="top", fill="both", expand=True)

tk.Label(info_frame, text="数据日志", font=("SimHei", 10, "bold")).pack(anchor="nw", pady=5)

# 数据日志文本区和滚动条
log_text_frame = tk.Frame(info_frame)
log_text_frame.pack(fill="both", expand=True)

log_text = tk.Text(log_text_frame, wrap="word")
log_text.pack(side='left', fill='both', expand=True)

log_scrollbar = tk.Scrollbar(log_text_frame, command=log_text.yview)
log_scrollbar.pack(side='right', fill='y')
log_text.config(yscrollcommand=log_scrollbar.set)

# 添加示例日志数据
sample_logs = [
    "[2023-10-15 08:30:22] 服务器已启动，监听端口 8080",
    "[2023-10-15 08:31:05] 客户端 192.168.1.105 已连接",
    "[2023-10-15 08:31:18] 接收来自客户端的数据: {\"type\":\"login\",\"user\":\"admin\"}",
    "[2023-10-15 08:31:45] 发送响应: {\"status\":\"success\",\"message\":\"登录成功\"}",
    "[2023-10-15 08:32:10] 接收来自客户端的数据: {\"type\":\"query\",\"id\":12345}",
    "[2023-10-15 08:32:15] 发送响应: {\"status\":\"success\",\"data\":[1,2,3,4,5]}",
    "[2023-10-15 08:33:02] 客户端请求断开连接",
    "[2023-10-15 08:33:05] 连接已关闭"
]

for log in sample_logs:
    log_text.insert(tk.END, log + "\n")

# 锁定日志区域，防止编辑
log_text.config(state=tk.DISABLED)

"""右边布局 数据发送"""
send_frame = tk.Frame(right_frame, height=200)
send_frame.pack(anchor="nw", fill="x", pady=10)


tk.Label(send_frame, text="数据发送", font=("SimHei", 10, "bold")).pack(anchor="nw", pady=5)

# 发送区域文本框和滚动条
send_text_frame = tk.Frame(send_frame)
send_text_frame.pack(fill="x", padx=5)

send_text = tk.Text(send_text_frame, height=5, wrap="word")
send_text.pack(side='left', fill='x', expand=True)

send_scrollbar = tk.Scrollbar(send_text_frame, command=send_text.yview)
send_scrollbar.pack(side='right', fill='y')
send_text.config(yscrollcommand=send_scrollbar.set)

# 添加示例发送数据
sample_send_data = '''{
    "type": "command",
    "action": "get_status",
    "parameters": {
        "id": 1001,
        "timeout": 30
    }
}'''
send_text.insert(tk.END, sample_send_data)

# 发送按钮
tk.Button(send_frame, text="发       送", bg='green', fg='white', height=1).pack(fill='x', padx=5, pady=5)

root.mainloop()
